﻿using BLL.Base;
using CommandService;
using Entity;
using Entity.DbEntity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.Statistics
{
    public class ProductStockBLL : PubMethod
    {
        Commands cmd;
        LoginInfo userLogin;
        ReturnResult rsl;
        public ProductStockBLL()
        {
            cmd = new Commands();
            userLogin = new LoginInfo();
        }
        /// <summary>
        /// 实时仓库库存
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ReturnResult ProductStockInfo(string page, string limit, ref string pageCount, string condition)
        {
            rsl = new ReturnResult();

            try
            {
                #region 检索条件
                StringBuilder sb = new StringBuilder();
                #region  拼表名
                string whWhere = "";string whereSql = "";
                sb.Append("select proCode, proName,spec,SUM(CONVERT(decimal(18, 1), proWeight)) AS number, COUNT(barCode) AS barCount ,");
                sb.Append(" wareHouse, houseName, barStatus  from V_BarCode_Info ");
                sb.Append("WHERE barStatus = 'need-out'  AND status = 'Y' ");

                if (!string.IsNullOrWhiteSpace(condition))
                {
                    var json = JsonConvert.DeserializeObject<JObject>(condition);
                    foreach (JProperty obj in json.Properties())
                    {
                        if (!string.IsNullOrWhiteSpace(obj.Value.ToString()))
                        {
                            if (obj.Name == "wareHouse")
                            {
                                whWhere = "wareHouse='"+ obj.Value.ToString() + "'";
                                continue;
                            }
                            if (!string.IsNullOrWhiteSpace(whereSql))
                            {
                                sb.Append(" and " + obj.Name + " like '%" + obj.Value.ToString() + "%' ");
                            }
                        }
                    }
                }

                if (!string.IsNullOrWhiteSpace(whWhere))
                {
                    sb.Append(" and " + whWhere);
                }
                sb.Append("GROUP BY proCode, proName,spec, wareHouse, houseName, barStatus");
                #endregion
                var tableName = sb.ToString();
                var orderSql = " number desc ";
                #endregion

                var list = cmd.TableToDictionary(cmd.PageTableList(tableName, page, limit, whereSql, orderSql, ref pageCount, ref pageCount));


                rsl.Data = list;
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;

        }

        /// <summary>
        /// 实时库存明细
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ReturnResult StockDetail(string condition)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select * from V_BarCode_Info where status='Y' and barStatus ='need-out' ");
                #region 检索条件
                if (!string.IsNullOrWhiteSpace(condition))
                {
                    var json = JsonConvert.DeserializeObject<JObject>(condition);
                    if (!string.IsNullOrWhiteSpace(json["proCode"].ToString()))
                    {
                        sb.Append(" and proCode='" + json["proCode"].ToString() + "'");
                    }
                    if (!string.IsNullOrWhiteSpace(json["wareHouseName"].ToString()) && json["wareHouseName"].ToString()!="选择仓库" )
                    {
                        sb.Append(" and wareHouseName='" + json["wareHouseName"].ToString() + "'");
                    }
                }
                #endregion
                var table = cmd.GetDataTable(sb.ToString());

                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 库存客户
        /// </summary>
        /// <returns></returns>
        public ReturnResult StockCustomer()
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("select distinct customCode,customName from V_ProductPlan_info where plCode in(");
                sb.Append("select distinct planCode from bar_ProductBarCode_info ");
                sb.Append("where barStatus='need-out' )");
                var table = cmd.GetDataTable(sb.ToString());
                sb.Clear();
                sb.Append("select distinct isnull(c_customer,'') customName,isnull(cu.cuCode,'') customCode ");
                sb.Append(" from bar_ProductBarCode_info b left join fun_Customs_info cu on cu.cuName=b.c_customer ");
                sb.Append("and cu.trType='2'");
                var cuTable = cmd.GetDataTable(sb.ToString());
                var cTable = table.Copy();
                foreach (DataRow row in cuTable.Rows)
                {
                    var dr = table.Select("customCode='" + row["customCode"].ToString() + "'");
                    if (dr.Length < 1)
                    {
                        table.Rows.Add(row.ItemArray);
                    }
                }
                rsl.Data = cmd.TableToDictionary(table);
                rsl.Result = true;
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 库存占用明细添加
        /// </summary>
        /// <param name="info">关键参数</param>
        /// <param name="contents">占用内容</param>
        /// <returns></returns>
        public ReturnResult AddStockZhanYong(string info, string contents)
        {
            rsl = new ReturnResult();
            try
            {
                if (string.IsNullOrWhiteSpace(info))
                {
                    rsl.Message = "关键参数丢失";
                    return rsl;
                }
                var arr = info.Split(',');
                DbProductStockMark m = new DbProductStockMark();
                m.contents = contents;
                if (string.IsNullOrWhiteSpace(arr[3]))
                {
                    m.customCode = "-";
                }
                else
                {
                    m.customCode = arr[3];
                }
                if (string.IsNullOrWhiteSpace(arr[1]))
                {
                    m.xilie = "-";
                }
                else
                {
                    m.xilie = arr[1];
                }
                if (string.IsNullOrWhiteSpace(arr[2]))
                {
                    m.spec = "-";
                }
                else
                {
                    m.spec = arr[2];
                }
                m.proCode = arr[0];
                m.createtime = getNow();
                m.createuser = userLogin.UserInfo().userCode;
                rsl = cmd.Add(m);
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }
        /// <summary>
        /// 删除库存占用
        /// </summary>
        /// <param name="dataId"></param>
        /// <returns></returns>
        public ReturnResult DelStockZhanYong(string dataId)
        {
            rsl = new ReturnResult();
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("delete from pi_ProductStock_mark where id=" + dataId);
                rsl = cmd.UpdateSql(sb.ToString());
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;
        }

        /// <summary>
        /// 获取库存占用数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public ReturnResult getStockZhanYong(string info)
        {
            rsl = new ReturnResult();
            try
            {
                if (string.IsNullOrWhiteSpace(info))
                {
                    rsl.Message = "关键参数丢失";
                    return rsl;
                }
                var arr = info.Split(',');
                string msg = "";
                var table = this.StockZhanYongTable(arr[0], arr[1], ref msg);
                if (string.IsNullOrWhiteSpace(msg))
                {
                    var list = cmd.TableToDictionary(table);
                    rsl.Data = list;
                    rsl.Result = true;
                }
                else
                {
                    rsl.Message = msg;
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return rsl;

        }
        //public ReturnResult getStockZhanYong(string info)
        //{
        //    rsl = new ReturnResult();
        //    try
        //    {
        //        if (string.IsNullOrWhiteSpace(info))
        //        {
        //            rsl.Message = "关键参数丢失";
        //            return rsl;
        //        }
        //        var arr = info.Split(',');
        //        string msg = "";
        //        var table = this.StockZhanYongTable(arr[0], arr[1], arr[2], arr[3],ref msg);
        //        if (string.IsNullOrWhiteSpace(msg))
        //        {
        //            var list = cmd.TableToDictionary(table);
        //            rsl.Data = list;
        //            rsl.Result = true;
        //        }
        //        else
        //        {
        //            rsl.Message = msg;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        rsl.Message = ex.Message;
        //    }
        //    return rsl;

        //}

        //public ReturnResult getStockZhanYong(string info)

        /// <summary>
        /// 获取库存占用信息表
        /// </summary>
        /// <param name="proCode">存货编码</param>
        /// <param name="xilie">系列</param>
        /// <param name="spec">规格</param>
        /// <param name="customer">客户</param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        private DataTable StockZhanYongTable(string proCode, string wareHouse, ref string msg)
        {


            if (string.IsNullOrWhiteSpace(proCode))
            {
                msg = "存货编码不允许为空";
                return null;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("select o.* ,b.proCode,b.proName,b.number,b.unit,b.wareHouse ,b.spec,b.wareHouseName ");
            sb.Append("from pi_ProductOccupy_info o ");
            sb.Append("inner join V_BarCode_Info b  ");
            sb.Append("on b.barCode=o.barCode ");
            sb.Append("where proCode='" + proCode + "' and isdel=0");
            if (string.IsNullOrWhiteSpace(wareHouse))
            {
                sb.Append(" and wareHouse IS NULL");
            }
            else
            {
                sb.Append(" and wareHouse='" + wareHouse + "'");
            }
            return cmd.GetDataTable(sb.ToString());

        }
        //private DataTable StockZhanYongTable(string proCode, string xilie, string spec, string customer, ref string msg)
        //{
        //    if (string.IsNullOrWhiteSpace(customer))
        //    {
        //        customer = "-";
        //    }

        //    if (string.IsNullOrWhiteSpace(xilie))
        //    {
        //        xilie = "-";
        //    }

        //    if (string.IsNullOrWhiteSpace(spec))
        //    {
        //        spec = "-";
        //    }
        //    if (string.IsNullOrWhiteSpace(proCode))
        //    {
        //        msg = "存货编码不允许为空";
        //        return null;
        //    }
        //    StringBuilder sb = new StringBuilder();
        //    sb.Append("select * from pi_ProductStock_mark where ");
        //    sb.Append("proCode='" + proCode + "' and spec='" + spec + "' and xilie='" + xilie + "'");
        //    sb.Append(" and customCode='" + customer + "'");
        //    return cmd.GetDataTable(sb.ToString());
        //}



    }
}
